package com.glnz.spring6.resource;

import org.springframework.core.io.UrlResource;

import java.io.IOException;

/**
 * @Project ：learn_spring
 * @File ：UrlResourceDemo.java
 * @IDE ：IntelliJ IDEA
 * @Author ：自由如风间
 * @Date ：2025/4/5 22:05
 * @Description ：For study.
 */

public class UrlResourceDemo {

    public static void main(String[] args) {
        // http前缀
        loadUrlResource("https://www.baidu.com");

        // file前缀
        loadUrlResource("file:nothing.txt");
    }

    /**
     * 访问前缀http、file
     * @param path 网址或文件路径
     */
    public static void loadUrlResource(String path) {
        try {
            // 创建Resource实现类的对象UrlResource
            UrlResource url = new UrlResource(path);

            // 获取资源信息
            System.out.println(url.getFilename());
            System.out.println(url.getURL());
            System.out.println(url.getDescription());
            System.out.println(url.getInputStream().read());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
